#include <memory>

#include "Socket.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include "Protocol.hpp"


void Handler(Net_work::Socket* _Socket)
{
    while(true)
    {
        struct Request req;
        recv(_Socket->GetSockFd(),&req,sizeof req,0);
        req.Debug();
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage->  " << argv[0] << " port" << std::endl;
        return 0;
    }

    uint16_t localport = std::stoi(argv[1]);

    std::unique_ptr< TcpServer> Tcp_server = std::make_unique<TcpServer>(localport,Handler);

    Tcp_server->Loop();
}



// int main(int argc, char *argv[])
// {
//     if (argc != 2)
//     {
//         std::cout << "Usage->  " << argv[0] << " port" << std::endl;
//         return 0;
//     }

//     uint16_t localport = std::stoi(argv[1]);

//     Socket *listensock = new TcpSocket();

//     listensock->BuildListenSocket(localport, backlog);

//     while (true)
//     {
//         std::string peerip;
//         uint16_t peerport;
//         Socket *newsock = listensock->AcceptConnection(&peerip, &peerport);

//         std::cout << "get a new connection,sockfd: " << newsock->GetSockFd()
//                   << " client ifo: " << peerip << ":" << peerport << std::endl;

//         sleep(5);

//         newsock->CloseSocket();
//     }
// }